package com.devmindset.legoframework.usage.client.text;

import java.util.MissingResourceException;

import com.google.gwt.core.client.GWT;

/**
 * Init values and properties service.<br/><br/>
 * 
 * Constains convenient methods for the retrieval
 * of application initialization texts.
 * 
 * @author <a href="ice.penov@devmindset.com">Ice Penov</a>
 */
public final class InitValues {
	
	private static Init instance = null;
	
	private InitValues() {}
	
	public static synchronized Init get() {
		if (instance == null) {
			instance = (Init) GWT.create(Init.class);
		}
		return instance;
	}
	
  /**
   * Convenience method for text resource retrieval and fallback to the key name.
   * 
   * @param key the text resources key
   * 
   * @return the retrieved text if exists, the key value otherwise
   */
  public static String getString(String key) {
    String result = key;
    try {
      String text = get().getString(key);
      if (text != null) {
        result = text;
      }      
    } catch (MissingResourceException e) {
      ; //do nothing
    }
    return result;
  }
  
  /**
   * Convenience method to retrieve a text from the text resources and fallback
   * to a default value.
   * 
   * @param key the text resources key
   * @param defaultValue the default value
   * 
   * @return the retrieved text if exists, default value otherwise
   */
  public static String getString(String key, String defaultValue) {
    String result = defaultValue;
    try {
      String text = get().getString(key);
      if (text != null) {
        result = text;
      }      
    } catch (MissingResourceException e) {
      ; //do nothing
    }
    return result;
  }
	
}